package com.peanutplan.blog.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.peanutplan.blog.dao.SensitiveWordMapper;
import com.peanutplan.blog.exception.TipException;
import com.peanutplan.blog.model.vo.SensitiveWord;
import com.peanutplan.blog.model.vo.SensitiveWordExample;
import com.peanutplan.blog.service.ISensitiveWordsService;
import com.peanutplan.blog.utils.DateUtil;
import com.peanutplan.blog.utils.SensitiveWordUtil;

/**
 * Created by BlueT on 2017/3/16.
 */
@Service
public class SensitiveWordsServiceImpl implements ISensitiveWordsService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SensitiveWordsServiceImpl.class);

    @Resource
    private SensitiveWordMapper sensitiveWordMapper;
    
    @Resource
    private SensitiveWordUtil sensitiveWordUtil;
    
    @Override
    public void insertSensitiveWord(SensitiveWord sensitiveWordVo) {
        if (sensitiveWordVo.getSensitiveWord().length() < 2 || sensitiveWordVo.getSensitiveWord().length() > 2000) {
            throw new TipException("敏感词字数在2-2000个字符");
        }
        if (sensitiveWordVo.getReplaceWord().length() < 2 || sensitiveWordVo.getReplaceWord().length() > 2000) {
            throw new TipException("敏感词替换字符在2-2000个字符");
        }
        
        sensitiveWordVo.setCreated(DateUtil.getCurrentUnixTime());
        sensitiveWordMapper.insertSelective(sensitiveWordVo);
        sensitiveWordUtil.addSensitiveWordCache(sensitiveWordVo);
    }

    @Override
    public PageInfo<SensitiveWord> getSensitiveWordsWithPage(SensitiveWordExample sensitiveWordVoExample, int page, int limit) {

            PageHelper.startPage(page, limit);
            sensitiveWordVoExample = sensitiveWordVoExample==null?new SensitiveWordExample():sensitiveWordVoExample;
            sensitiveWordVoExample.setOrderByClause("created desc");
            List<SensitiveWord> sensitiveWords = sensitiveWordMapper.selectByExample(sensitiveWordVoExample);
            
            PageInfo<SensitiveWord> pages = new PageInfo<>(sensitiveWords);
            return pages;
    }

    @Override
    public SensitiveWord getSensitiveWordById(Integer id) {
        return sensitiveWordMapper.selectByPrimaryKey(id);
    }

    @Override
    public void delete(Integer id) {
    	SensitiveWord s = sensitiveWordMapper.selectByPrimaryKey(id);
    	if(s!=null) {
    		sensitiveWordUtil.removeSensitiveWordCache(s.getSensitiveWord());
    	}
        sensitiveWordMapper.deleteByPrimaryKey(id);
    }

	@Override
	public void update(SensitiveWord sensitiveWord) {
		SensitiveWord s = sensitiveWordMapper.selectByPrimaryKey(sensitiveWord.getId());
    	if(s!=null) {
    		sensitiveWordUtil.removeSensitiveWordCache(s.getSensitiveWord());
    		sensitiveWordUtil.addSensitiveWordCache(s);
    	}
    	
		sensitiveWordMapper.updateByPrimaryKeySelective(sensitiveWord);
	}

}
